package com.lt.bookkeeping.recycleView;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author LiuTong
 */
public class LoadMoreListen extends RecyclerView.OnScrollListener {
    public ILoadMore iLoadMore;
    public int counts ;
    public ITop iTop;

    public LoadMoreListen(ILoadMore iLoadMore,ITop iTop,int allSize) {
        this.iLoadMore= iLoadMore;
        counts = allSize;
        this.iTop = iTop;
    }

    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if(dy>0){
            /***
             *
             */
        }else if(dy < 0){

        }else {

        }
    }

    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
        assert lm != null;
        int topRecycleView = recyclerView.getChildCount() == 0 ?0:recyclerView.getChildAt(0).getTop();
        if(iTop != null){
            iTop.top(topRecycleView);
        }
        Log.e("距离top距离：",""+topRecycleView);
        int position = lm.findLastVisibleItemPosition();
        if(newState == RecyclerView.SCROLL_STATE_IDLE){
            if(position-1 < counts){
                if(iLoadMore != null){
                    iLoadMore.loadMore();
                }
            }
        }
    }
}
